<!DOCTYPE html>

<html>
<head>
  <title>configure.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>configure.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>CONFIGURE THE SERVER</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Define the basic configuration options for the node server, depending on
the current environment.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>module.<span class="function"><span class="title">exports</span></span> = (app, express) -&gt;</pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Load settings.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    database = require <span class="string">"./database.coffee"</span>
    logger = require <span class="string">"./logger.coffee"</span>
    mail = require <span class="string">"./mail.coffee"</span>
    settings = require <span class="string">"./settings.coffee"</span>
    twitter = require <span class="string">"./twitter.coffee"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Check if there are misconfigured settings on the app. Wrong settings will be logged to the console.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="function"><span class="title">checkSettings</span></span> = -&gt;</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Database.maxRetries must greater than 0. Will default to 1.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> settings.Database.maxRetries &lt; <span class="number">1</span>
            console.warn <span class="string">"Configure.checkSettings"</span>, <span class="string">"Database.maxRetries: <span class="subst">#{settings.Database.maxRetries}</span> is not valid."</span>, <span class="string">"Settings to 1"</span>
            settings.Database.maxRetries = <span class="number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>Email SMTP port must be a number greater than 0. Will default to 25.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> settings.Email.smtpPort? <span class="keyword">and</span> settings.Email.smtpPort &lt; <span class="number">0</span>
            console.warn <span class="string">"Configure.checkSettings"</span>, <span class="string">"Database.maxRetries: <span class="subst">#{settings.Database.maxRetries}</span> is not valid."</span>, <span class="string">"Settings to 1"</span>
            settings.Email.smtpPort = <span class="number">25</span></pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>Make it PaaS friendly. Check for environment variables to override specific settings.
If no environment variables are found, the app will use whatever is defined on the
<a href="settings.html">settings</a>. This is called ONLY if the <code>paas</code> web setting is true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="function"><span class="title">checkCloudEnvironment</span></span> = -&gt;</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Check for web (IP and port) variables.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        ip = process.env.OPENSHIFT_INTERNAL_IP
        port = process.env.OPENSHIFT_INTERNAL_PORT
        port = process.env.VCAP_APP_PORT <span class="keyword">if</span> <span class="keyword">not</span> port? <span class="keyword">or</span> port <span class="keyword">is</span> <span class="string">""</span>
        settings.Web.ip = ip <span class="keyword">if</span> ip? <span class="keyword">and</span> ip <span class="keyword">isnt</span> <span class="string">""</span>
        settings.Web.port = port <span class="keyword">if</span> port? <span class="keyword">and</span> port <span class="keyword">isnt</span> <span class="string">""</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Check for MongoDB variables.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        vcap = process.env.VCAP_SERVICES
        vcap = JSON.parse vcap <span class="keyword">if</span> vcap?
        <span class="keyword">if</span> vcap? <span class="keyword">and</span> vcap <span class="keyword">isnt</span> <span class="string">""</span>
            mongo = vcap[<span class="string">"mongodb-1.8"</span>]
            mongo = mongo[<span class="number">0</span>][<span class="string">"credentials"</span>] <span class="keyword">if</span> mongo?
            <span class="keyword">if</span> mongo?
                settings.Database.connString = <span class="string">"mongodb://<span class="subst">#{mongo.hostname}</span>:<span class="subst">#{mongo.port}</span>/<span class="subst">#{mongo.db}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Check for logging variables.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        logentriesToken = process.env.LOGENTRIES_TOKEN
        logglyToken = process.env.LOGGLY_TOKEN
        logglySubdomain = process.env.LOGGLY_SUBDOMAIN
        settings.Log.Logentries.token = logentriesToken <span class="keyword">if</span> logentriesToken? <span class="keyword">and</span> logentriesToken <span class="keyword">isnt</span> <span class="string">""</span>
        settings.Log.Loggly.token = logglyToken <span class="keyword">if</span> logglyToken? <span class="keyword">and</span> logglyToken <span class="keyword">isnt</span> <span class="string">""</span>
        settings.Log.Loggly.subdomain = logglySubdomain <span class="keyword">if</span> logglySubdomain? <span class="keyword">and</span> logglySubdomain <span class="keyword">isnt</span> <span class="string">""</span></pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>General configuration of the app (for all environments).</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.configure -&gt;
        checkSettings()
        checkCloudEnvironment() <span class="keyword">if</span> settings.Web.paas</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Init the logger.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        logger.init()</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Init modules.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        database.init()
        mail.init()
        twitter.init()</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Set view options, use Jade for HTML templates.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        app.set <span class="string">"views"</span>, settings.Path.viewsDir
        app.set <span class="string">"view engine"</span>, <span class="string">"jade"</span>
        app.set <span class="string">"view options"</span>, { layout: <span class="literal">false</span> }</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Express settings.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        app.use express.bodyParser()
        app.use express.cookieParser settings.Web.cookieSecret
        app.use express.compress()
        app.use express.methodOverride()
        app.use express[<span class="string">"static"</span>] settings.Path.publicDir
        app.use express.favicon __dirname + <span class="string">"/../favicon.ico"</span>
        app.use app.router</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>Connect assets and dynamic compiling.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        ConnectAssets = (require <span class="string">"connect-assets"</span>) settings.ConnectAssets
        app.use ConnectAssets</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Configure development environment.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.configure <span class="string">"development"</span>, -&gt;
        app.use express.errorHandler settings.ErrorHandling</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>Configure production environment.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.configure <span class="string">"production"</span>, -&gt;
        app.use express.errorHandler()</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
